package org.iqiyi.rabbitMq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@RefreshScope
@Configuration
public class RabbitMQConfig {
    public static final String EXCHANGE_OPEN = "exchange";
    public static final String QUEUE_NAME = "queue";
    public static final String KEY = "key";
    @Value(value = "${spring.rabbitmq.host}")
    private String host;
    @Value(value = "${spring.rabbitmq.port}")
    private int port;
    @Value(value = "${spring.rabbitmq.username}")
    private String username;
    @Value(value = "${spring.rabbitmq.password}")
    private String password;
    @Value(value = "${spring.rabbitmq.virtual-host}")
    private String virtualHost;


    /**
     * 配置链接信息
     *
     * @return
     */
    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        connectionFactory.setPublisherConfirms(true); // 必须要设置
        return connectionFactory;
    }
    /**
     * 交换机
     * */
    @Bean
    public DirectExchange videoExchange() {
        return new DirectExchange(EXCHANGE_OPEN, true, false);
    }


    @Bean
    public Queue videoQueue() {
        return new Queue(QUEUE_NAME, true); //队列持久

    }
    /**
     * 队列与交换机进行绑定
     * */
    @Bean
    public Binding videoBind() {
        return BindingBuilder.bind(videoQueue()).to(videoExchange()).with(RabbitMQConfig.KEY);
    }
}
